package com.example.databaseproject;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;

import java.util.ArrayList;

public class DataBase extends AppCompatActivity {

    private Button create,add,delete,update,find;
    myDataBase helper;
    SQLiteDatabase database;
    private GridView gridView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data_base);
        create = findViewById(R.id.catDataBase);
        add = findViewById(R.id.add);
        delete = findViewById(R.id.delete);
        update = findViewById(R.id.change);
        find = findViewById(R.id.find);
        gridView = findViewById(R.id.gridView);
    }

    public void setCreate(View view){
        helper = new myDataBase(this,"mabq",1);
        //将自动调用myDatabase中的creat方法
        database =helper.getReadableDatabase();
    }

    public void add(View view){
        ContentValues contentValues = new ContentValues();
        contentValues.put("name","校长");
        contentValues.put("age",45);
        Long tempNumber= database.insert("school",null,contentValues);
        if (tempNumber == -1){
            Toast.makeText(DataBase.this,"插入失败",Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(DataBase.this,"插入成功",Toast.LENGTH_SHORT).show();
        }
    }
    public void delete(View view){
        int temp = database.delete("school","id = ?",new String[]{"2"});
        Toast.makeText(this,temp+"删除了",Toast.LENGTH_SHORT).show();
    }
    public void update(View view){
        try {
            ContentValues contentValues = new ContentValues();
            contentValues.put("name","汤姆");
            contentValues.put("age",9999);
            int row= database.update("school",contentValues,"name =?",new String[]{"Tom"});
            Toast.makeText(this,row+"更新了",Toast.LENGTH_SHORT).show();
        }catch (Exception e){
            AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setTitle("错误信息")
                    .setMessage(e.getMessage())
                    .setPositiveButton("确定",null).show();
        }

    }
    public void find(View view){
        Cursor cursor = database.query("school",null,null,null,null,null,null);
        if (cursor.moveToFirst()){
            ArrayList list = new ArrayList();
            do {
                String name =cursor.getString(cursor.getColumnIndex("name"));
                int age =cursor.getInt(cursor.getColumnIndex("age"));
                Log.d("Tag",name + " " + age);
                list.add(name);
                list.add(age);
            }while (cursor.moveToNext());
            myAdapter adapter = new myAdapter(this,list);
            gridView.setAdapter(adapter);
        }
    }
}